﻿using Framework.Code.Const;
using Framework.Models;
using Framework.Models.Entities;
using Framework.Utility.Config;
using Framework.Utility.Extensions;
using Framework.Utility.Helper;
using Microsoft.Extensions.Logging; 

namespace Framework.Code.FlowWork
{
    public class FlowNotificaiton
    {
        private readonly ILogger<FlowNotificaiton> _logger;

        public FlowNotificaiton(ILogger<FlowNotificaiton> logger)
        {
            _logger = logger;
        }

        public Dictionary<int, Action<Fw_Flowinstance, IList<Approver>, string>> ApprovalNotifyActionDict
        {

            get
            {
                var dict = new Dictionary<int, Action<Fw_Flowinstance, IList<Approver>, string>>
                {
                    { DocumentType.LeaveDoc, (e, a, s) => SendLeaveApprovalNotification(e, a, s) }, 
                };

                return dict;
            }
        }


        public void SendLeaveApprovalNotification(Fw_Flowinstance doc, IList<Approver> approvers, string remark)
        {
            //if (!(doc is Biz_LeaveTime leave)) throw new ArgumentException("单据类型错误");

            if (approvers == null) throw new ArgumentNullException("approvers");
             
            string createBy = doc.ApplyUserName + doc.ApplyUserId;

            const string tempalteName = "_leave_approval_notify.html";

            foreach (var receiver in approvers)
            {
                if (!receiver.IsOnduty) continue;
                if (string.IsNullOrEmpty(receiver.Email) || !receiver.Email.IsEmail())
                {
                    _logger.LogInformation("发送审批通知邮件失败，{0}({1})的邮件地址有误".FormatWith(receiver.UserName, receiver.Email));
                    continue;
                }

                var jumpUrl = "wwww.baidu.com";

                var mailInfo = new Sys_MailInfo
                {
                    To = receiver.Email,
                    Subject = $" 审批通知(系统邮件)",
                    Body = FileHelper.GetTemp(tempalteName).FormatWith(
                        DateTime.Now.ToShortDateString(),
                        $"{receiver.UserName} ({receiver.SerialNumber})",
                        "CaseNo",
                        $"{receiver.UserName} ({receiver.SerialNumber})",
                        "Department",//GetEmployeeCurrentDepartment(applicant.Department),
                        "LeaveType",//GetLeaveTypeText(leave.LeaveType),
                        $"{DateTime.Now.AddDays(-1).Formatted("yyyy/MM/dd HH:mm")} - {DateTime.Now.Formatted("yyyy/MM/dd HH:mm")}",
                        $"8 h",
                        "Description",
                        remark,
                        jumpUrl
                        , string.Empty), 
                }; 


            }
        }
    }
}
